package cn.stylefeng.guns.modular.account.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.account.entity.Account;
import cn.stylefeng.guns.modular.account.model.params.AccountParam;
import cn.stylefeng.guns.modular.account.model.result.AccountResult;
import cn.stylefeng.guns.modular.account.service.AccountService;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;


/**
 * 账户表控制器
 *
 * @author liuqiang
 * @Date 2020-02-10 12:50:10
 */
@RestController
@RequestMapping("/account")
public class AccountController{

    @Autowired
    private AccountService accountService;

    /**
     * 新增接口
     *
     * @author liuqiang
     * @Date 2020-02-10
     */
    @PostMapping("/addItem")
    public ResponseData addItem(@RequestBody AccountParam accountParam) {
        if(StringUtils.isBlank(accountParam.getPhone())){
            throw new ServiceException(BizExceptionEnum.PARAM_MISSING.getCode(),"手机号不能为空");
        }

        if(StringUtils.isBlank(accountParam.getSchoolName())){
            throw new ServiceException(BizExceptionEnum.PARAM_MISSING.getCode(),"学校名称不能为空");
        }

        this.accountService.add(accountParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author liuqiang
     * @Date 2020-02-10
     */
//    @RequestMapping("/editItem")
//    @ResponseBody
    public ResponseData editItem(AccountParam accountParam) {
        this.accountService.update(accountParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author liuqiang
     * @Date 2020-02-10
     */
//    @RequestMapping("/delete")
//    @ResponseBody
    public ResponseData delete(AccountParam accountParam) {
        this.accountService.delete(accountParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author liuqiang
     * @Date 2020-02-10
     */
//    @RequestMapping("/detail")
//    @ResponseBody
    public ResponseData detail(AccountParam accountParam) {
        Account detail = this.accountService.getById(accountParam.getId());
        return ResponseData.success(detail);
    }

    @GetMapping("/getBalance")
    public ResponseData getBalance(AccountParam accountParam) {
        if(StringUtils.isBlank(accountParam.getPhone())){
            throw new ServiceException(BizExceptionEnum.PARAM_MISSING.getCode(),"phone为空");
        }

        AccountResult detail = this.accountService.getByPhone(accountParam.getPhone());
        if(detail != null){
            Map<String ,Object> map = new HashMap<>();
            map.put("balance",detail.getBalance());
            return ResponseData.success(map);
        }

        return ResponseData.success();
    }

    @GetMapping("/remindList")
    public LayuiPageInfo getRemindList(AccountParam accountParam) {
        return this.accountService.findPageRemindList(accountParam);
    }

    /**
     * 查询列表
     *
     * @author liuqiang
     * @Date 2020-02-10
     */
    @GetMapping("/list")
    public LayuiPageInfo list(AccountParam accountParam) {
        return this.accountService.findPageBySpec(accountParam);
    }

    /**
     * 查询列表
     *
     * @author liuqiang
     * @Date 2020-02-10
     */
    @GetMapping("/statistics")
    public LayuiPageInfo statistic(AccountParam accountParam) {
        return this.accountService.statistic(accountParam);
    }
}


